*** Are the U.S. Military's Nonpartisan Norms Eroding? ***
*** Trent J. Lythgoe, University of Kansas Political Science ***
*** CCES Data as of 28 JUL 20 ***
*** Stata 17 ***

**# Setup 

* Required plugins
	* outreg2
	* asdoc
	* st0378_1.pkg (Regression Models for Truncated Distributions)
	* spost13_ado (SPost13 commands from Long and Freese (2014)
	
* Download the following CCES data files from https://cces.gov.harvard.edu/
	* 2008 cces_2008_common.dta
	* 2010 cces_2010_common_validated.dta
	* 2012 CCES12_Common_VV.dta
	* 2014 CCES14_Common_Content_Validated.dta
	* 2016 CCES16_Common_OUTPUT_Feb2018_VV.dta 
	* 2018 cces18_common_vv.dta
	
* Place CCES data in working directory
* Place graph formatting files (Graph_Rec2.grec) in working directory

**# Variable coding 
* weight = Survey Weight 

** Indepdendent Variable
* mil = Military 

** Dependent Variables
* part = Partisan 
* pid_str = Partisanship strength 
* activism = attend + sign + work + donate

** Political Controls
* pid7 = Partisanship (7 categories)
* ideo5 = Ideology
* ideo_str = Ideology strength
* pol_int = Political interest
* pol_know = Political knowledge

** Demographic Controls
* age = Age 
* gender2 = Gender 
* race2 = Race (dichotomous)
* edu2 = College degree (dichotomous)
* income = Family income (categorical)
* relig = Religiosity

** Other variables (for descriptive tables)
* pid3 = Partisanship (3 categories)

**# Merge CCES Data
quietly{

clear
* Set working directory for data
cd "C:\Users\tlyth\OneDrive\Desktop\AFS Paper\Replication"

* 2008
use "cces_2008_common.dta"

keep V201 V203 V207 V208 V211 V213 V216 V217 CC308a CC308b V243 V244 V246 CC307a CC326 CC328_1 CC328_3 CC403 CC415_1 CC415_3 CC415_4 CC415_6

gen yr08 = 1
gen year = 2008
sum yr08

*2010
append using "cces_2010_common_validated.dta", generate(yr10) keep(V101 V203 V207 V208 V211 V213 V216 V217 CC309a CC309b V243 V244 V246 V212d CC354 V252 V254 CC401 CC417a_1 CC417a_2 CC417a_3 CC417a_4) nolabel

sum yr10
replace year = 2010 if yr10 == 1
tab year

*2012
append using "CCES12_Common_VV.dta", generate(yr12) keep(V103 votereg birthyr gender race educ ideo5 newsint faminc pid7 pew_religimp pew_churatd CC309a CC309b CC354 milstat_1 milstat_3 CC401 CC417a_1 CC417a_2 CC417a_3  CC417a_4) nolabel

sum yr12
replace year = 2012 if yr12 == 1
tab year

* 2014
* Note: weight variable mislabeled V103 in guide
append using "CCES14_Common_Content_Validated.dta", generate(yr14) keep(weight votereg birthyr gender race educ ideo5 newsint faminc pid7 pew_religimp pew_churatd CC14_309a CC14_309b CC354 milstat_1 milstat_3 CC401 CC417a_1 CC417a_2 CC417a_3 CC417a_4) nolabel

sum yr14
replace year = 2014 if yr14 == 1
tab year

* 2016
append using "CCES16_Common_OUTPUT_Feb2018_VV.dta", generate(yr16) keep(commonweight commonweight_post votereg birthyr gender race educ ideo5 newsint faminc pid7 pew_religimp pew_churatd CC16_321a CC16_321b CC16_364 milstat_1 milstat_3 CC16_401 CC16_417a_1 CC16_417a_2 CC16_417a_3 CC16_417a_4) nolabel

sum yr16
replace year = 2016 if yr16 == 1
tab year

*2018
append using "cces18_common_vv.dta", generate(yr18) keep(commonweight commonpostweight votereg birthyr gender race educ ideo5 newsint faminc_new pid7 pew_religimp pew_churatd CC18_309a CC18_309b CC18_350 milstat_1 milstat_3 CC18_401 CC18_417a_1 CC18_417a_2 CC18_417a_3 CC18_417a_4) nolabel

sum yr18
replace year = 2018 if yr18 == 1
tab year
}

**# Variable Coding
quietly{
**# Independent Variable
* Military status = mil
* We'd like to know whether you or someone in your immediate family is currently serving or has ever served in the U.S. military.  Immediate family is defined as your parents, siblings, spouse, and children.  Please check all boxes that apply.
* Military Household - I am
* <1>	I am currently serving in the U.S. military.
* <2>	No
* <8>	Skipped
* <9>	Not Asked
tab CC328_1 year
tab V252 year
tab milstat_1 year
egen mil = rowmax(CC328_1 V252 milstat_1)
replace mil = 0 if mil != 1
label variable mil "Military"
label define mil 0 "Civilian" 1 "Military"
label values mil mil
tab mil year

**# Dependent Variables**
* Partisanship = pid7
* Pre Election 7 point Party ID
* Note: Coded from branching questions 
* <1>	Strong Democrat
* <2> 	Not very strong Democrat
* <3> 	Lean Democrat
* <4> 	Independent
* <5> 	Lean Republican
* <6> 	Not very strong Republican
* <7>	Strong Republican
* <8> 	Not sure
* <98> 	Skipped
* <99> 	Not Asked
tab CC307a year
tab V212d year
tab pid7 year
rename pid7 pid7_orig
egen pid7 = rowmax(CC307a V212d pid7_orig)
replace pid7 =. if pid7 > 7
label variable pid7
label variable pid7 "Partisanship"
label define pid7 1 "Strong Democrat" 2 "Weak Democrat" 3 "Lean Democrat" 4 "Independent" 5 "Lean Republican" 6 "Weak Republican" 7 "Strong Republican", modify
label values pid7 pid7
tab pid7 year

* Partisanship 3-level
gen pid3 =.
replace pid3 = 1 if pid7 >= 1 & pid7 <=3
replace pid3 = 2 if pid7 == 4
replace pid3 = 3 if pid7 >= 5 & pid7 <=7
label variable pid3 "Partisanship"
label define pid3 1 "Democrat" 2 "Indepdendent" 3 "Republican"
label values pid3 pid3
tab pid3 year

* Partisan (H1)
tab pid7 year
gen indep =.
replace indep = 1 if pid7 == 4
replace indep = 0 if pid7 >=1 & pid7 <=3
replace indep = 0 if pid7 >=5 & pid7 <=7
label variable indep "Independent"
label define indep 0 "Partisan" 1 "Independent"
label values indep indep
tab indep year

tab indep, nolabel
gen part = abs(indep -1)
label variable part "Partisan"
label define part 0 "Indepdendent" 1 "Partisan"
label values part part
tab part

* Partisan identity strength (H2)
tab pid7 year
gen pid_str =.
replace pid_str = 0 if pid7 == 4
replace pid_str = 1 if pid7 == 3 | pid7 == 5
replace pid_str = 2 if pid7 == 2 | pid7 == 6
replace pid_str = 3 if pid7 == 1 | pid7 == 7
label variable pid_str "Partisan strength"
label define pid_str 0 "Independent" 1 "Leaning" 2 "Weak" 3 "Strong"
label values pid_str pid_str
tab pid_str year

* Political activism 
* Note: For the next four variables:
* 1 yes/selected
* 2 no/not selected
* >2 skipped/not asked/no data
* In the past year did you...(check all the apply)
* Attend political meetings = attend
tab CC415_1 year
tab CC417a_1 year
tab CC16_417a_1 year
tab CC18_417a_1 year
egen attend = rowmax(CC415_1 CC417a_1 CC16_417a_1 CC18_417a_1)
replace attend = 0 if attend == 2
label variable attend "Attended a political meeting"
label values attend ny
tab attend year

* Put up political sign = sign
tab CC415_3 year
tab CC417a_2 year
tab CC16_417a_2 year
tab CC18_417a_2 year
egen sign = rowmax(CC415_3 CC417a_2 CC16_417a_2 CC18_417a_2)
replace sign = 0 if sign == 2
label variable sign "Put up a political sign"
label values sign ny
tab sign year

* Work for candidate/campaign = work
tab CC415_4 year
tab CC417a_3 year
tab CC16_417a_3 year
tab CC18_417a_3 year
egen work = rowmax(CC415_4 CC417a_3 CC16_417a_3 CC18_417a_3)
replace work = 0 if work == 2
label variable work "Worked for a candidate or campaign"
label values work ny
tab work year

* Donate to candidate, campaign, or political org = donate
tab CC415_6 year
tab CC417a_4 year
tab CC16_417a_4 year
tab CC18_417a_4 year
egen donate = rowmax(CC415_6 CC417a_4 CC16_417a_4 CC18_417a_4)
replace donate = 0 if donate == 2
label variable donate "Donated money to politcs"
label values donate ny
tab donate year

* Political Activism (Count) (H3)
gen activism =.
replace activism = attend + sign + work + donate
label variable activism "Political activism"
label define activism 0 "None" 1 "Low" 2 "Moderate" 3 "High" 4 "Very High"
label values activism activism
tab activism year

**# Control Variables**

* Age (birth year) = age
* Birth year
* In what year were you born?
* Require integer value between 1900 and 2000.
tab year, sum(V207)
tab year, sum (birthyr)
egen birth_yr = rowmax(V207 birthyr)
tab year, sum(birth_yr)

gen age = year - birth_yr - 1
label variable age "Age"
tab year, sum(age)

* Age Category (for tables)
gen age_cat2 = age
recode age_cat2 (min/25=1) (26/30=2) (31/35=3) (36/40=4) (41/max=5)
tab age_cat2
label variable age_cat2 "Age"
label define age 1 "<= 25" 2 "26-30" 3 "31-35" 4 "36-40" 5 ">= 41" 
label values age_cat2 age
tab age_cat2

* Gender = gender2
* Gender
* Are you male or female?
* <1>	Male
* <2>	Female
* <8>	Skipped
* <9>	Not Asked
tab V208
tab gender
egen gender2 = rowmax(V208 gender)
tab gender2 year
replace gender2 = abs(1 - gender2)
label variable gender2 "Gender"
label define gender2 0 "Male" 1 "Female"
label values gender2 gender2
tab gender2 year

* Race = race2
* Race
* Which racial or ethnic group best describes you?
* <1>	White
* <2>	Black or African-American
* <3>	Hispanic or Latino
* <4>	Asian or Asian-American
* <5>	Native American
* <6>	Mixed Race
* <7>	Other (please specify) [Open]
* <8>	Middle Eastern
* <98>	Skipped
* <99>	Not Asked
tab V211
tab race
egen race8 = rowmax(V211 race)
tab race8 year

gen race2 =.
replace race2 = 0 if race8 == 1
replace race2 = 1 if race8 > 1
replace race2 =. if race8 > 8
label variable race2 "Race"
label define race2 0 "White" 1 "Non-White"
label values race2 race2
tab race2 year

* Education = edu2
* Education
* What is the highest level of education you have completed?
* <1>	Did not graduate from High School
* <2>	High school graduate
* <3>	Some college, but no degree (yet)
* <4>	2-year college degree
* <5>	4-year college degree 
* <6>	Post-graduate degree (MA, MBA, MD, JD, PhD, etc.)
* <8>	Skipped
* <9>	Not Asked
tab V213
tab educ
egen edu6 = rowmax(V213 educ)
replace edu6 =. if edu6 > 6
label variable edu6 "Education"
label define edu6 1 "No HS" 2 "HS graduate" 3 "Some college" 4 "2-year degree" 5 "4-year degree" 6 "Post-grad degree"
label values edu6 edu6
tab edu6 year

tab edu6, nolabel
gen edu2 =.
replace edu2 = 0 if edu6 <=4
replace edu2 = 1 if edu6 >=5
label variable edu2 "Education"
label define edu2 0 "No Degree" 1 "College Degree"
label values edu2 edu2
tab edu2

* Ideology = ideo5
* Ideology
* Thinking about politics these days, how would you describe your own political viewpoint?
* <1>	Very Liberal
* <2>	Liberal
* <3>	Moderate
* <4>	Conservative
* <5>	Very Conservative
* <6>	Not sure
* <8>	Skipped
* <9>	Not Asked
tab V243 year
tab ideo5 year
rename ideo5 ideo5_orig
egen ideo5 = rowmax(ideo5_orig V243)
replace ideo5 =. if ideo5 > 5
label variable ideo5 "Ideology"
label define ideo5 1 "Very Liberal" 2 "Liberal" 3 "Moderate" 4 "Conservative" 5 "Very Conservative", modify
label values ideo5 ideo5
tab ideo5 year

* Ideology strength
tab ideo5 year
gen ideo_str =.
replace ideo_str = 0 if ideo5 == 3
replace ideo_str = 1 if ideo5 ==2 | ideo5 == 4
replace ideo_str = 2 if ideo5 ==1 | ideo5 == 5
label variable ideo_str "Ideology strength"
label define ideo_str 0 "Moderate" 1 "Weak Ideology" 2 "Strong Ideology"
label values ideo_str ideo_str
tab ideo_str year
 
* Political interest = pol_int
* Interest in news and public affairs
* Some people seem to follow what's going on in government and public affairs most of the time, whether there's an election going on or not.  Others aren't that interested.  Would you say you follow what's going on in government and public affairs…?
* <1>	Most of the time
* <2>	Some of the time
* <3>	Only now and then
* <4>	Hardly at all
* <5>	Don't know (2008-2010)
* <7>  Don't know (2012-2018)
* <8>	Skipped
* <9>	Not Asked
tab V244 year
tab newsint year
egen pol_int = rowmax(V244 newsint)
replace pol_int =. if pol_int > 4
replace pol_int = 5 - pol_int
label variable pol_int "Political interest"
label define pol_int 4 "High" 3 "Moderate" 2 "Low" 1 "Very Low"
label values pol_int pol_int
tab pol_int year

* Create dichotomous political interest variable for inflation variable (Table A8)
gen pol_int2 = .
replace pol_int2 = 0 if pol_int == 1 | pol_int == 2
replace pol_int2 = 1 if pol_int == 3 | pol_int == 4
tab pol_int2 activism

* Family Income = income
* Family income
* Thinking back over the last year, what was your family's income?
* 2008-2010
* <1>	less than $10,000
* <2>	$10,000-$14,999
* <3>	$15,000-$19,999
* <4>	$20,000-$24,999
* <5>	$25,000-$29,999
* <6>	$30,000-$39,999
* <7>	$40,000-$49,999
* <8>	$50,000-$59,999
* <9>	$60,000-$69,999
* <10>	$70,000-$79,999
* <11>	$80,000-$99,999
* <12>	$100,000-$119,999
* <13>	$120,000-$149,999
* <14>	$150,000 or more
* <15>	Prefer not to say
* <98>	Skipped
* <99>	Not Asked

* 2012-2018
* 1 Less than $10,000
* 2 $10,000 - $19,999
* 3 $20,000 - $29,999
* 4 $30,000 - $39,999
* 5 $40,000 - $49,999
* 6 $50,000 - $59,999
* 7 $60,000 - $69,999
* 8 $70,000 - $79,999
* 9 $80,000 - $99,999
* 10 $100,000 - $119,999
* 11 $120,000 - $149,999
* 12 $150,000 - $199,999
* 13 $200,000 - $249,999
* 14 $250,000 - $349,999
* 15 $350,000 - $499,999
* 16 $500,000 or more
* 97 Prefer not to say

* Note: I do not adjust 2008-2010 for the 2012-2018 scale. Thus, the income category shows R income relative to the same year, but should not be used for comparisons between years.
* 2012-2016 measured income > $150K two ways. Rs who indicated $150K or more are counted as $150-$199K.  Rs who indicated $250K or more are counted as $250-$349K.
tab V246 year
tab faminc year
tab faminc_new year
egen income = rowmax(V246 faminc faminc_new)
replace income = 12 if income == 31
replace income = 14 if income == 32
replace income =. if income == 15 & year == 2008
replace income =. if income == 15 & year == 2010
replace income =. if income >32
label variable income "Income"
tab income year

* Religiousity = relig 

* Importance of religion
* How important is religion in your life?
* <1>	Very Important
* <2>	Somewhat Important
* <3>	Not too important
* <4>	Not at all important
* <8>	Skipped
* <9>	Not Asked
tab V216 year
tab pew_religimp year
egen relig_imp = rowmax(V216 pew_religimp)
replace relig_imp =. if relig_imp > 4
replace relig_imp = 5 - relig_imp
label variable relig_imp "Importance of religion"
label define relig_imp 1 "Not at all important" 2 "Not too important" 3 "Somewhat important" 4 "Very important"
label values relig_imp relig_imp
tab relig_imp year

* Church attendance 
* Aside from weddings and funerals, how often do you attend religious services?
* <1>	More than once a week
* <2>	Once a week
* <3>	Once or twice a month
* <4>	A few times a year
* <5>	Seldom
* <6>	Never
* <7>	Don't know
* <8>	Skipped
* <9>	Not Asked
tab V217 year
tab pew_churatd year
egen church_attend = rowmax(V217 pew_churatd)
replace church_attend =. if church_attend > 7
replace church_attend = 7 - church_attend
label variable church_attend "Church Attendance"
label define church_attend 1 "Never" 2 "Seldom" 3 "A few times a year" 4 "Once or twice a month" 5 "Once a week" 6 "More than once a week"
label values church_attend church_attend
tab church_attend year

// egen z_relig_imp = std(relig_imp)
// egen z_church_attend = std(church_attend)
// gen relig = z_relig_imp + z_church_attend
// tabulate year, summarize(relig)

factor church_attend relig_imp, pcf
predict relig
label variable relig "Religiosity"
tabulate year, summarize(relig)

* Political knowledge = pol_know
* Which party has a majority of seats in the...?
* Party Majority: House of Representatives
* <1> 	Republicans
* <2>  Democrats 
* <3> 	Neither 
* <4> 	Not Sure
* <8> 	Skipped
* <9> 	Not Asked
tab CC308a year
tab CC309a year
tab CC14_309a year
tab CC16_321a year
tab CC18_309a year

gen house08 = CC308a
gen house10 = CC309a if year == 2010
gen house12 = CC309a if year == 2012
gen house14 = CC14_309a
gen house16 = CC16_321a
gen house18 = CC18_309a

egen pkh = rowmax(house08 house10 house12 house14 house16 house18)
replace pkh =. if pkh > 4
replace pkh = 0 if pkh <= 4
replace pkh = 1 if house08 == 2 | house10 == 2 | house12 == 1 | house14 == 1 | house16 == 1 | house18 == 1
label variable pkh "Political knowledge - US House majority"
label define pk 0 "Incorrect" 1 "Correct"
label values pkh pk
tab pkh year

* Party Majority: Senate
* <1> 	Republicans 
* <2> 	Democrats 
* <3> 	Neither 
* <4> 	Not Sure
* <8> 	Skipped
* <9> 	Not Asked
tab CC308b year
tab CC309b year
tab CC14_309b year
tab CC16_321b year
tab CC18_309b year

gen senate08 = CC308b
gen senate10 = CC309b if year == 2010
gen senate12 = CC309b if year == 2012
gen senate14 = CC14_309b
gen senate16 = CC16_321b
gen senate18 = CC18_309b

egen pks = rowmax(senate08 senate10 senate12 senate14 senate16 senate18)
replace pks =. if pks > 4
replace pks = 0 if pks <= 4
replace pks = 1 if senate08 == 2 | senate10 == 2 | senate12 == 2 | senate14 == 2 | senate16 == 1 | senate18 == 1
label variable pks "Political knowledge - US Senate majority"
label values pks pk
tab pks year

* Additive index
gen pol_know = (pkh + pks) / 2
label variable pol_know "Political knowledge"
tabulate year, sum(pol_know)

* Survey Weight = weight
rename weight wt
egen weight = rowmax(V201 V101 wt V103 commonweight)
tabulate year, summarize(weight)
egen post_weight = rowmax(commonweight_post commonpostweight)
replace post_weight = weight if post_weight ==.
tabulate year, summarize(post_weight)
}

** Save data
save "CCES_clean_coded.dta"
use "CCES_clean_coded.dta"
**# Descriptive Tables and Figure
quietly{

* Set survey weight
svyset _n, weight(weight) vce(linearized) singleunit(missing)

* Set graph scheme
set scheme lean1

* Table A1 Variables Summary
asdoc sum part pid_str activism pid7 ideo5 ideo_str pol_int pol_know age gender race2 edu2 income relig, replace label title(Variables Summary) dec(2) save(table_a1)

* Table A12 Mil and Civ Respondents
asdoc tab mil year, column replace label title(Military Status) dec(2) save(Table_A12)

* Table_1 Compare Mil and Civ Demographics
log using table_1, replace
table ( var ) ( mil ) () [pweight = weight], statistic(fvpercent gender2 race2 edu2 age_cat2) nototal
log close
translate table_1.smcl table_1.pdf, replace

* Table A11 Mil and Civ Party Identification
log using table_a11, replace
table ( var ) ( year ) ( mil ) [pweight = weight], statistic(fvpercent pid3) nototal
log close
translate table_a11.smcl table_a11.pdf, replace

**# Figure 1 Military and civilian partisanship
gen dem=0
replace dem=1 if pid3==1

gen ind=0
replace ind=1 if pid3==2

gen rep=0
replace rep=1 if pid3==3

graph bar (mean) rep ind dem [pweight = weight] if mil==0, over(year, label(labsize(large))) percentage stack blabel(bar, format(%9.1f) position(center) size(large) color(black)) ///
bar(1, color(maroon%50)) bar(2, color(gs8%50)) bar(3, color(navy%50)) ///
ysize(4) xsize(6) ///
title("Civilians", size(vlarge)) ///
ytitle("Percent", size(large)) ///
ylabel(, labsize(large) nogrid) ///
legend(yoffset(-2) label(1 "Republican") label(2 "Independent") label(3 "Democrat") position(6) rows(1) bmargin(small) size(medlarge) region(lcolor(black))) ///
saving(fig_1_a, replace)

graph bar (mean) rep ind dem [pweight = weight] if mil==1, over(year, label(labsize(large))) percentage stack blabel(bar, format(%9.1f) position(center) size(large) color(black)) ///
bar(1, color(maroon%50)) bar(2, color(gs8%50)) bar(3, color(navy%50)) ///
ysize(4) xsize(6) ///
title("Military", size(vlarge)) ///
ytitle("Percent", size(large)) ///
ylabel(, labsize(large) nogrid) ///
legend(yoffset(-2) label(1 "Republican") label(2 "Independent") label(3 "Democrat") position(6) rows(1) bmargin(small) size(medlarge) region(lcolor(black))) ///
saving(fig_1_b, replace) 

gr combine fig_1_a.gph fig_1_b.gph, col(1) ysize(6) iscale(.6)
graph export fig_1.png, as(png) replace
}

** Hypothesis testing

**# * Hypothesis 1 (H1): Military servicemembers are becoming more partisan. 
quietly{
* Table A2
log using Table_A2, replace
table mil year [pweight = weight], statistic(mean part) nformat (%9.2f)
log close
translate Table_A2.smcl table_a2.pdf, replace

* Figure 2
collapse (mean) part [pweight=weight], by(mil year)
replace part = part*100

twoway (connected part year if mil==0, color(blue) msymbol(O) msize(.8)) ///
(connected part year if mil==1, color(green) msymbol(D) msize(.8)) ///
(scatter part year if mil==1 & year==2008 | mil==0 & year==2018, msymbol(i) mlabel(part) mlabf(%9.1f) mlabp(6) mlabg(2) mlabs(medium)) ///
(scatter part year if mil==0 & year==2008 | mil==1 & year==2018, msymbol(i) mlabel(part) mlabf(%9.1f) mlabp(12) mlabg(2) mlabs(medium)) ///
, ///
plotregion(margin(large)) ///
ysize(4) xsize(6) ///
title("", size(medlarge)) ///
ytitle("% partisan", size(medlarge)) ///
ylabel(70(10)100, labsize(medlarge)) ///
yscale(range(70(10)100)) ///
xtitle("Year", size(medlarge) yoffset(-1.5)) ///
xlabel(, labsize(medlarge)) ///
legend(order(1 2) label(1 "Civilian") label(2 "Military") label(3 "") label(4 "") position(6) rows(1) bmargin(small) size(medlarge) region(lcolor(black))) 

graph export fig_2.png, as(png) replace

* Reload data and reset survey weight
use "CCES_clean_coded.dta", clear
svyset _n, weight(weight) vce(linearized) singleunit(missing)

* Table 2 + Table A5
svy linearized : logistic part mil##(year) edu2 age ideo5 ideo_str pol_int pol_know gender2 race2 income relig 
outreg2 using table_2_a5.doc, replace ctitle(1) label alpha (.001, .01, .05) dec(3) 

svy linearized : logistic part mil##(year edu2) age ideo5 ideo_str pol_int pol_know gender2 race2 income relig 
outreg2 using table_2_a5.doc, append ctitle(2) label alpha (.001, .01, .05) dec(3) 

svy linearized : logistic part mil##(year c.age) edu2 ideo5 ideo_str pol_int pol_know gender2 race2 income relig 
outreg2 using table_2_a5.doc, append ctitle(3) label alpha (.001, .01, .05) dec(3) 

svy linearized : logistic part mil##(year edu2 c.age) ideo5 ideo_str pol_int pol_know gender2 race2 income relig 
outreg2 using table_2_a5.doc, append ctitle(4) label alpha (.001, .01, .05) dec(3) 

* Figure 3 H1 Marginal Effects
margins, at(age=(20(5)55)) over(mil) atmeans vsquish
marginsplot, recastci(rarea) ///
ysize(4) xsize(6) ///
title("") ///
ytitle("Probability of identifying as a partisan", size(medlarge)) ///
ylabel(, labsize(medlarge)) ///
xtitle("Age", size(medlarge)) ///
xlabel(, labsize(medlarge)) ///
legend(position(6) rows(1) bmargin(small) size(medlarge) region(lcolor(black))) ///
plot1opts(msymbol(o) color(blue) lpattern(dash)) ///
ci1opts(lcolor(gs6) lwidth(thin) fcolor(gs4%20)) ///
plot2opts(msymbol(d) color(green)) ///
ci2opts(lcolor(gs6) lwidth(thin) fcolor(gs4%20))
graph export fig_3.png, as(png) replace
}

**# Hypothesis 2 (H2): SMs party identification strengthening
quietly{
* Table A3
log using Table_A3, replace
table mil year [pweight = weight] if pid_str > 0, statistic(mean pid_str) nformat(%9.2f)
log close
translate Table_A3.smcl table_a3.pdf, replace

* Figure 4
drop if pid_str==0
collapse (mean) pid_str [pweight=weight], by(mil year)

twoway (connected pid_str year if mil==0, color(blue) msymbol(O) msize(.8)) ///
(connected pid_str year if mil==1, color(green) msymbol(D) msize(.8)) ///
(scatter pid_str year if mil==0 & year==2008 | mil==0 & year==2018, color(green) msymbol(i) mlabel(pid_str) mlabf(%9.2f) mlabp(6) mlabg(2) mlabs(medium)) ///
(scatter pid_str year if mil==1 & year==2008 | mil==1 & year==2018, msymbol(i) mlabel(pid_str) mlabf(%9.1f) mlabp(12) mlabg(2) mlabs(medium)) ///
, ///
plotregion(margin(large)) ///
ysize(4) xsize(6) ///
title("", size(medlarge)) ///
ytitle("Mean partisan identity strength", size(medlarge)) ///
ylabel(1 2 3, labsize(medlarge)) ///
yscale(range(1 2 3)) ///
xtitle("Year", size(medlarge) yoffset(-1.5)) ///
xlabel(, labsize(medlarge)) ///
legend(order(1 2) label(1 "Civilian") label(2 "Military") label(3 "") label(4 "") position(6) rows(1) bmargin(small) size(medlarge) region(lcolor(black))) 

graph export fig_4.png, as(png) replace

* Reload data
* Reload data and reset survey weight
use "CCES_clean_coded.dta", clear
svyset _n, weight(weight) vce(linearized) singleunit(missing)

* Table 3 + Table A6
svy linearized : ologit pid_str mil##(year) edu2 age pid7 ideo5 ideo_str pol_int pol_know gender2 race2 income relig if pid_str > 0 
outreg2 using table_3_A6.doc, replace ctitle(1) label alpha (.001, .01, .05) dec(3) 

svy linearized : ologit pid_str mil##(year edu2) age i.year pid7 ideo5 ideo_str pol_int pol_know gender2 race2 income relig if pid_str > 0
outreg2 using table_3_A6.doc, append ctitle(2) label alpha (.001, .01, .05) dec(3) 

svy linearized : ologit pid_str mil##(year c.age) edu2 pid7 ideo5 ideo_str pol_int pol_know gender2 race2 income relig if pid_str > 0
outreg2 using table_3_A6.doc, append ctitle(3) label alpha (.001, .01, .05) dec(3) 

svy linearized : ologit pid_str mil##(year edu2 c.age) pid7 ideo5 ideo_str pol_int pol_know gender2 race2 income relig if pid_str > 0
outreg2 using table_3_A6.doc, append ctitle(4) label alpha (.001, .01, .05) dec(3) 
}

**# Hypothesis 3 (H3): servicemembers becoming more politically active.
quietly{
* Table A4
log using Table_A4, replace
table mil year [pweight = weight], statistic(mean activism) nformat(%9.2f)
log close
translate Table_A4.smcl table_A4.pdf, replace

*Figure 5
collapse (mean) activism [pweight=weight], by(mil year)

twoway (connected activism year if mil==0, color(blue) msymbol(O) msize(.8)) ///
(connected activism year if mil==1, color(green) msymbol(D) msize(.8)) ///
(scatter activism year if mil==0 & year==2008 | mil==0 & year==2018, msymbol(i) mlabel(activism) mlabf(%9.2f) mlabp(6) mlabg(2) mlabs(medium)) ///
(scatter activism year if mil==1 & year==2008 | mil==1 & year==2018, msymbol(i) mlabel(activism) mlabf(%9.2f) mlabp(12) mlabg(2) mlabs(medium)) ///
, ///
plotregion(margin(large)) ///
ysize(4) xsize(6) ///
title("", size(medlarge)) ///
ytitle("Mean # of political activities", size(medlarge)) ///
ylabel(0 1 2, labsize(medlarge)) ///
yscale(range(0 1 2)) ///
xtitle("Year", size(medlarge) yoffset(-1.5)) ///
xlabel(, labsize(medlarge)) ///
legend(order(1 2) label(1 "Civilian") label(2 "Military") label(3 "") label(4 "") position(6) rows(1) bmargin(small) size(medlarge) region(lcolor(black))) 

graph export fig_5.png, as(png) replace

* Reload data and reset survey weight
use "CCES_clean_coded.dta", clear
svyset _n, weight(weight) vce(linearized) singleunit(missing)

* Figure A1
histogram activism, fraction barwidth(.8) ytitle(Density) xtitle(Number of Political Activities)
graph export fig_a1.png, as(png) replace

* Check for political activism variable overdispersion
sum activism, detail
* variable is over-dispersed

*Table 4 + Table A7
* DV ranges from 0 to 4, so right truncation is set to 5
trncregress activism mil##(year) edu2 age pid7 pid_str ideo5 ideo_str pol_int pol_know gender2 race2 income relig [pweight=weight], dist(negbin) rtrunc(5) vce(robust)
outreg2 using table_4_A7.doc, replace ctitle(1) label alpha (.001, .01, .05) dec(3) 

trncregress activism mil##(year edu2) age i.year pid7 pid_str ideo5 ideo_str pol_int pol_know gender2 race2 income relig [pweight=weight], dist(negbin) rtrunc(5) vce(robust)
outreg2 using table_4_A7.doc, append ctitle(2) label alpha (.001, .01, .05) dec(3) 

trncregress activism mil##(year c.age) edu2 pid7 pid_str ideo5 ideo_str pol_int pol_know gender2 race2 income relig [pweight=weight], dist(negbin) rtrunc(5) vce(robust)
outreg2 using table_4_A7.doc, append ctitle(3) label alpha (.001, .01, .05) dec(3) 

trncregress activism mil##(year edu2 c.age) pid7 pid_str ideo5 ideo_str pol_int pol_know gender2 race2 income relig [pweight=weight], dist(negbin) rtrunc(5) vce(robust)
outreg2 using table_4_A7.doc, append ctitle(4) label alpha (.001, .01, .05) dec(3) 

margins, at(age=(20(5)55)) over(mil) atmeans
marginsplot, recastci(rarea) ///
ysize(4) xsize(6) ///
title("") ///
ytitle("Predicted number of activities", size(medlarge)) ///
ylabel(, labsize(medlarge)) ///
xtitle("Age", size(medlarge)) ///
xlabel(, labsize(medlarge)) ///
legend(position(6) rows(1) bmargin(small) size(medlarge) region(lcolor(black))) ///
plot1opts(msymbol(o) color(blue) lpattern(dash)) ///
ci1opts(lcolor(gs6) lwidth(thin) fcolor(gs4%20)) ///
plot2opts(msymbol(d) color(green)) ///
ci2opts(lcolor(gs6) lwidth(thin) fcolor(gs4%20))
graph export fig_6.png, as(png) replace
}

**# Additional appendix tables and figures
quietly{
* Table A10 Wald Tests, mil vs. civ activism by year
log using Table_A10, replace
svy linearized : mean activism if mil==0, over(year)
svy linearized : mean activism if mil==1, over(year)
 
svy: mean activism if year == 2008, over(mil)
test _b[c.activism@0bn.mil] = _b[c.activism@1.mil]

svy: mean activism if year == 2010, over(mil)
test _b[c.activism@0bn.mil] = _b[c.activism@1.mil]

svy: mean activism if year == 2012, over(mil)
test _b[c.activism@0bn.mil] = _b[c.activism@1.mil]

svy: mean activism if year == 2014, over(mil)
test _b[c.activism@0bn.mil] = _b[c.activism@1.mil]

svy: mean activism if year == 2016, over(mil)
test _b[c.activism@0bn.mil] = _b[c.activism@1.mil]

svy: mean activism if year == 2018, over(mil)
test _b[c.activism@0bn.mil] = _b[c.activism@1.mil]
log close
translate table_a10.smcl table_a10.pdf, replace

* Table A9 count model comparison
trncregress activism mil##(year edu2 c.age) pid7 pid_str ideo5 ideo_str pol_int pol_know gender2 race2 income relig [pweight=weight], dist(negbin) rtrunc(5) vce(robust)
outreg2 using table_A9.doc, replace ctitle(NB TRUNC) label alpha (.001, .01, .05) dec(3) 

svy linearized : nbreg activism mil##(year edu2 c.age) pid7 pid_str ideo5 ideo_str pol_int pol_know gender2 race2 income relig 
outreg2 using table_A9.doc, append ctitle(NB NO TRUNC) label alpha (.001, .01, .05) dec(3) 

svy linearized : zinb activism mil##(year edu2 c.age) pid7 pid_str ideo5 ideo_str pol_know gender2 race2 income relig, inflate(pol_int2) 
outreg2 using table_A9.doc, append ctitle(ZINB Political Intrest) label alpha (.001, .01, .05) dec(3) 

svy linearized : regress activism mil##(year edu2 c.age) pid7 pid_str ideo5 ideo_str pol_int pol_know gender2 race2 income relig 
outreg2 using table_A9.doc, append ctitle(OLS) label alpha (.001, .01, .05) dec(3) 

svy linearized : ologit activism mil##(year edu2 c.age) pid7 pid_str ideo5 ideo_str pol_int pol_know gender2 race2 income relig 
outreg2 using table_A9.doc, append ctitle(OLOGIT) label alpha (.001, .01, .05) dec(3) 


* Table A8 NB and ZINB comparison using political interest as inflation variable
clear all
cd "C:\Users\tlyth\OneDrive\Desktop\AFS Paper\Replication"
use "CCES_clean_coded"
log using Table_A8, replace
countfit activism mil##(year edu2 c.age) pid7 pid_str ideo5 ideo_str pol_know gender2 race2 income relig, inf(pol_int2) nbreg zinb forcevuong nograph
log close
translate Table_A8.smcl table_A8.pdf, replace
}
	
**# Directory cleanup
local txtfiles: dir . files "*.txt"
foreach txt in `txtfiles' {
    erase `"`txt'"'
}

local smclfiles: dir . files "*.smcl"
foreach smcl in `smclfiles' {
    erase `"`smcl'"'
}

local gphfiles: dir . files "*.gph"
foreach gph in `gphfiles' {
    erase `"`gph'"'
}
** END FILE **
	



	
